import { _decorator, Color, Component, Label, Node, Sprite } from 'cc';
import { aa } from '../frame/FrameCore';
import sceneMgr from '../manager/SceneMgr';
import oh from './OhCtrl';
const { ccclass, property } = _decorator;


@ccclass('LoadCtrl')
export class LoadCtrl extends Component {

    @property(Sprite) load: Sprite;
    @property(Sprite) bg: Sprite;
    @property(Node) loadBg:Node;
    @property(Label) loadLabel: Label;
    private _isload = true;
    private _fill = 0;
    private _num = 0;
    private _isDone = false;

    protected onEnable(): void {
        this.bg.color = oh.getDarkState()?Color.WHITE:new Color(40,52,96,255);
        this.loadScene();
    }
    async loadScene(){
        await sceneMgr.loadFirst();
        this._isDone = true;
    }

    close() {
        sceneMgr.loadHome();
        this._isload = false;
        aa.utils.clearUI(this.node);
    }

    update(dt: number) {
        if (!this._isload) return;
        const rate = aa.res.loadingRate;
        this._fill +=dt*5;
        if(this._fill>rate){
            this._fill = rate;
        }
        const num = Math.floor(this._fill*100);
        if(num>this._num){
            this.loadLabel.string = (num>9?"":"0") + Math.min(100,num);
            this._num = num;
        }
        this.load.fillRange = this._fill;
        if(num>=100 && this._isDone){
            this.close();
        }
    }
}

